﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;

#endregion


namespace Freeform
{
    /// <summary>
    /// Represents an Entity that exists in a 2D world.
    /// </summary>
    public class Entity2D : Entity, IGeometric2D
    {

        #region Members

        /// <summary>
        /// Gets the 2D actor for this entity.
        /// </summary>
        public Actor2D Actor { get; internal set; }


        /// <summary>
        /// Gets or sets the position of the Entity.
        /// </summary>
        public Vector2 Position { get; set; }


        /// <summary>
        /// Gets or sets the orientation of the Entity.
        /// </summary>
        public Single Orientation { get; set; }


        /// <summary>
        /// Gets or sets the size of the Entity.
        /// </summary>
        public Vector2 Size { get; set; }


        /// <summary>
        /// Gets whether or not the Mouse is hovering over the 2d entity in any of the World's viewports.
        /// </summary>
        public Boolean IsMouseOver
        {
            get
            {
                foreach (Viewport View in World.Viewports)
                {
                    Viewport2D View2D = View as Viewport2D;
                    if (View2D == null) continue;

                    Vector2 MouseLoc = View2D.MouseLocation_World;
                    Vector2 Diff = MouseLoc - Position;
                    Single AbsX = Math.Abs(Diff.X);
                    Single AbsY = Math.Abs(Diff.Y);

                    if (AbsX < Size.X / 2 && AbsY < Size.Y / 2) return true;
                }
                return false;
            }
        }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new entity and automatically links it to the given world.
        /// </summary>
        /// <param name="World">The world that this entity belongs to.</param>
        public Entity2D(World World) 
            : base(World)
        {
            Position = Vector2.Zero;
            Size = Vector2.One;
        }

        #endregion


        #region Update


        #endregion


        #region Draw

        /// <summary>
        /// Allows the entity to draw itself.
        /// </summary>
        /// <param name="Time">Provides a snapshot of timing values.</param>
        /// <param name="Viewport">The viewport to render to.</param>
        public override void Draw(GameTime Time, Viewport Viewport)
        {
            base.Draw(Time, Viewport);

            if (Actor == null) return;

            Viewport2D View = Viewport as Viewport2D;
            if (View == null) return;

            Actor.Draw(View, Time);
        }

        #endregion


        #region Dispose


        #endregion


        #region Utility


        #endregion

    }
}